import useSWR from 'swr';
import { statisticsOrderApi, StatisticsOrderDTO } from '../api/dashboard';
import { useAuthStore2 } from '../store/useAuthStore2';
import { useSpecialStatisticsStore } from '../store/useSpecialStatisticsStore';

export const useStatisticsData = () => {
  const token = useAuthStore2((s) => s.token);
  const { data, error, isLoading } = useSWR(
    () => (token ? '/report/large-screen/gyl-statisticsChart' : null),
    statisticsOrderApi,
  );

  const specialStatisticsStore = useSpecialStatisticsStore.getState();

  const getSeriesData = (
    data: string[],
    list: StatisticsOrderDTO[],
    name: string,
  ) => {
    return data.map((item) => {
      //找出当前时间点的操作
      const dateOp = list.filter((i) => i.operTimeWeek === item);
      //找出当前时间点的操作
      return dateOp.find((i) => i.operLogConvert === name)?.countNum || 0;
    });
  };

  //统计数据
  const statisticsData: { [key: string]: number } = {
    '采购订单数': data?.receiptAddNum || 0,
    '采购总金额': data?.receiptUploadNum || 0,
    '全球仓库面积': data?.shipmentAddNum || 0,
    '全球库存数': data?.shipmentOutNum || 0,
    '在途库存数': specialStatisticsStore?.inventorySkuSpecs || 0,
  };

  // 统计数据- 临时demo
  const statisticsDataDemo: { [key: string]: number } = {
    '采购订单数': data?.otherMaps?.poNums || 0,
    '采购单总金额': data?.otherMaps?.poAmount || 0,
    '在途库存数': data?.otherMaps?.onWayInventoryNums || 0,
    '总出库量': data?.otherMaps?.shipmentOrderTotalNums || 0,
    '在库库存数-SKU': data?.otherMaps?.invertorySkus || 0,
    '在库库存数-PCS': data?.otherMaps?.invertorySkuPcs || 0,
  };

  // 统计数据- 临时demo -cx
  const statisticsDataDemoCx: { [key: string]: number } = {
    '采购订单数': data?.otherMaps?.poNums || 0,
    '采购单总金额': data?.otherMaps?.poAmount || 0,
    '采购未入库': data?.otherMaps?.onWayInventoryNums || 0,
    '总出库量': data?.otherMaps?.shipmentOrderTotalNums || 0,
    '在库库存数-SKU': data?.otherMaps?.invertorySkus || 0,
    '在库库存数-PCS': data?.otherMaps?.invertorySkuPcs || 0,
  };

  //出库趋势图数据聚合
  const shipmentTendencyList = data?.shipmentTendencyList || [];
  let shipmentTendencyListMap: any = {};
  shipmentTendencyList.forEach((item) => {
    shipmentTendencyListMap[item?.operTimeWeek || ''] = {
      name: item?.operLogConvert || '',
      value: item?.countNum || 0,
    };
  });

  const shipmentTendencyData: {
    xAxis: string[];
    seriesData: {
      name: string;
      data: number[];
      areaTopColor: string;
      labelColor: string;
      lineStyle: string;
    }[];
  } = {
    xAxis: [],
    seriesData: [
      {
        name: '接单量',
        data: [],
        areaTopColor: '#D8D8D8',
        labelColor: '#D8D8D8',
        lineStyle: '#D8D8D8',
      },
      {
        name: '已拣选量',
        data: [],
        areaTopColor: 'rgba(226, 143, 37, 1)',
        labelColor: 'rgba(226, 143, 37, 1)',
        lineStyle: 'rgba(226, 143, 37, 1)',
      },
      {
        name: '已出库量',
        data: [],
        areaTopColor: 'rgba(3, 182, 195, 1)',
        labelColor: 'rgba(3, 182, 195, 1)',
        lineStyle: 'rgba(3, 182, 195, 1)',
      },
    ],
  };

  shipmentTendencyData.xAxis = Object.keys(shipmentTendencyListMap).sort(
    function (a, b) {
      return (
        new Date(a + ':00:00').getTime() - new Date(b + ':00:00').getTime()
      );
    },
  );

  let pathname = typeof window !== 'undefined' && window.location.pathname;
  if (pathname && pathname.includes('/web/cx')) {
    // 城西仓 没有 拣选量
    shipmentTendencyData.seriesData.splice(1, 1);
    shipmentTendencyData.seriesData[0].data = getSeriesData(
      shipmentTendencyData?.xAxis,
      shipmentTendencyList,
      '接单量（出库）',
    );
    shipmentTendencyData.seriesData[1].data = getSeriesData(
      shipmentTendencyData?.xAxis,
      shipmentTendencyList,
      '已出库量',
    );
  } else { 
     shipmentTendencyData.seriesData[0].data = getSeriesData(
       shipmentTendencyData?.xAxis,
       shipmentTendencyList,
       '接单量（出库）',
     );
     shipmentTendencyData.seriesData[1].data = getSeriesData(
       shipmentTendencyData?.xAxis,
       shipmentTendencyList,
       '已拣选量',
     );
     shipmentTendencyData.seriesData[2].data = getSeriesData(
       shipmentTendencyData?.xAxis,
       shipmentTendencyList,
       '已出库量',
     );
  }
 

  const receiptTendencyList = data?.receiptTendencyList || [];
  // 入库单数据聚合
  let receiptTendencyListMap: any = {};
  receiptTendencyList.forEach((item) => {
    receiptTendencyListMap[item?.operTimeWeek || ''] = {
      name: item?.operLogConvert || '',
      value: item?.countNum || 0,
    };
  });

  const receiptTendencyData: {
    xAxis: string[];
    seriesData: {
      name: string;
      data: number[];
      areaTopColor: string;
      labelColor: string;
      lineStyle: string;
    }[];
  } = {
    xAxis: [],
    seriesData: [
      {
        name: '接单量',
        data: [],
        areaTopColor: '#D8D8D8',
        labelColor: '#D8D8D8',
        lineStyle: '#D8D8D8',
      },
      {
        name: '已收货量',
        data: [],
        areaTopColor: 'rgba(226, 143, 37, 1)',
        labelColor: 'rgba(226, 143, 37, 1)',
        lineStyle: 'rgba(226, 143, 37, 1)',
      },
      {
        name: '已上架量',
        data: [],
        areaTopColor: 'rgba(3, 182, 195, 1)',
        labelColor: 'rgba(3, 182, 195, 1)',
        lineStyle: 'rgba(3, 182, 195, 1)',
      },
    ],
  };

  receiptTendencyData.xAxis = Object.keys(receiptTendencyListMap).sort(
    function (a, b) {
      return (
        new Date(a + ':00:00').getTime() - new Date(b + ':00:00').getTime()
      );
    },
  );
  receiptTendencyData.seriesData[0].data = getSeriesData(
    receiptTendencyData?.xAxis,
    receiptTendencyList,
    '接单量（入库）',
  );
  receiptTendencyData.seriesData[1].data = getSeriesData(
    receiptTendencyData?.xAxis,
    receiptTendencyList,
    '已收货量',
  );
  receiptTendencyData.seriesData[2].data = getSeriesData(
    receiptTendencyData?.xAxis,
    receiptTendencyList,
    '已上架量',
  );

  return {
    statisticsData: statisticsData,
    statisticsDataDemo: statisticsDataDemo,
    statisticsDataDemoCx: statisticsDataDemoCx,
    shipmentTendencyData,
    receiptTendencyData,
    isLoading,
    isError: error,
  };
};
